
// <copyright file="DynamicDAO.Delete.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The dynamic dao.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Data.Dynamic
{
    using System.Collections.Generic;
    using System.Data;

    /// <summary>
    /// The dynamic dao.
    /// </summary>
    /// <typeparam name="E">
    /// </typeparam>
    public static partial class DynamicDAO<E>
    {
        #region Public Methods and Operators

        /// <summary>
        /// The delete.
        /// </summary>
        /// <param name="entities">
        /// The entities.
        /// </param>
        /// <param name="tableName">
        /// The table name.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public static int Delete(IEnumerable<E> entities, string tableName = null)
        {
            IDbCommand command = getCommand();
            populateValuesDelegate method = initializeCommandForDml(command, DmlOperationTypes.Delete, tableName);
            return executeTransactionCommand(command, entities, false, method);
        }

        /// <summary>
        /// The delete.
        /// </summary>
        /// <param name="entities">
        /// The entities.
        /// </param>
        /// <param name="connection">
        /// The connection.
        /// </param>
        /// <param name="tableName">
        /// The table name.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public static int Delete(IEnumerable<E> entities, IDbConnection connection, string tableName = null)
        {
            IDbCommand command = connection.CreateCommand();
            populateValuesDelegate method = initializeCommandForDml(command, DmlOperationTypes.Delete, tableName);
            return executeCommand(command, entities, false, method);
        }

        /// <summary>
        /// The delete.
        /// </summary>
        /// <param name="entities">
        /// The entities.
        /// </param>
        /// <param name="transaction">
        /// The transaction.
        /// </param>
        /// <param name="tableName">
        /// The table name.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public static int Delete(IEnumerable<E> entities, IDbTransaction transaction, string tableName = null)
        {
            IDbCommand command = transaction.Connection.CreateCommand();
            command.Transaction = transaction;
            populateValuesDelegate method = initializeCommandForDml(command, DmlOperationTypes.Delete, tableName);
            return executeCommand(command, entities, false, method);
        }

        /// <summary>
        /// The delete.
        /// </summary>
        /// <param name="entity">
        /// The entity.
        /// </param>
        /// <param name="tableName">
        /// The table name.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public static int Delete(E entity, string tableName = null)
        {
            IDbCommand command = getCommand();
            initializeCommandForDml(command, DmlOperationTypes.Delete, tableName);
            return executeCommandAndCloseConnection(command, false);
        }

        /// <summary>
        /// The delete.
        /// </summary>
        /// <param name="entity">
        /// The entity.
        /// </param>
        /// <param name="connection">
        /// The connection.
        /// </param>
        /// <param name="tableName">
        /// The table name.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public static int Delete(E entity, IDbConnection connection, string tableName = null)
        {
            IDbCommand command = connection.CreateCommand();
            initializeCommandForDml(command, DmlOperationTypes.Delete, tableName);
            return executeCommand(command, false);
        }

        /// <summary>
        /// The delete.
        /// </summary>
        /// <param name="entity">
        /// The entity.
        /// </param>
        /// <param name="transaction">
        /// The transaction.
        /// </param>
        /// <param name="tableName">
        /// The table name.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public static int Delete(E entity, IDbTransaction transaction, string tableName = null)
        {
            IDbCommand command = transaction.Connection.CreateCommand();
            command.Transaction = transaction;
            initializeCommandForDml(command, DmlOperationTypes.Delete, tableName);
            return executeCommand(command, false);
        }

        /// <summary>
        /// The delete.
        /// </summary>
        /// <param name="whereClause">
        /// The where clause.
        /// </param>
        /// <param name="tableName">
        /// The table name.
        /// </param>
        /// <param name="whereClauseConditions">
        /// The where clause conditions.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public static int Delete(string whereClause, string tableName = null, params ClauseCondition[] whereClauseConditions)
        {
            IDbCommand command = getCommand();
            command.Parameters.Clear();
            command.CommandText = tableName == null ? _deleteCommand : string.Format(_deleteCommandFormat, tableName);
            initializeCommandWithWhereClause(command, whereClause, whereClauseConditions);
            return executeCommandAndCloseConnection(command, false);
        }

        /// <summary>
        /// The delete.
        /// </summary>
        /// <param name="whereClause">
        /// The where clause.
        /// </param>
        /// <param name="connection">
        /// The connection.
        /// </param>
        /// <param name="tableName">
        /// The table name.
        /// </param>
        /// <param name="whereClauseConditions">
        /// The where clause conditions.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public static int Delete(string whereClause, IDbConnection connection, string tableName = null, params ClauseCondition[] whereClauseConditions)
        {
            IDbCommand command = connection.CreateCommand();
            command.Parameters.Clear();
            command.CommandText = tableName == null ? _deleteCommand : string.Format(_deleteCommandFormat, tableName);
            initializeCommandWithWhereClause(command, whereClause, whereClauseConditions);
            return executeCommand(command, false);
        }

        /// <summary>
        /// The delete.
        /// </summary>
        /// <param name="whereClause">
        /// The where clause.
        /// </param>
        /// <param name="transaction">
        /// The transaction.
        /// </param>
        /// <param name="tableName">
        /// The table name.
        /// </param>
        /// <param name="whereClauseConditions">
        /// The where clause conditions.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public static int Delete(string whereClause, IDbTransaction transaction, string tableName = null, params ClauseCondition[] whereClauseConditions)
        {
            IDbCommand command = transaction.Connection.CreateCommand();
            command.Transaction = transaction;
            command.Parameters.Clear();
            command.CommandText = tableName == null ? _deleteCommand : string.Format(_deleteCommandFormat, tableName);
            initializeCommandWithWhereClause(command, whereClause, whereClauseConditions);
            return executeCommand(command, false);
        }

        /// <summary>
        /// The delete all.
        /// </summary>
        /// <param name="tableName">
        /// The table name.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public static int DeleteAll(string tableName = null)
        {
            IDbCommand command = getCommand();
            command.CommandText = "DELETE FROM " + (tableName ?? TableName);
            return executeCommandAndCloseConnection(command, false);
        }

        /// <summary>
        /// The delete all.
        /// </summary>
        /// <param name="connection">
        /// The connection.
        /// </param>
        /// <param name="tableName">
        /// The table name.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public static int DeleteAll(IDbConnection connection, string tableName = null)
        {
            IDbCommand command = connection.CreateCommand();
            command.CommandText = "DELETE FROM " + (tableName ?? TableName);
            return executeCommand(command, false);
        }

        /// <summary>
        /// The delete all.
        /// </summary>
        /// <param name="transaction">
        /// The transaction.
        /// </param>
        /// <param name="tableName">
        /// The table name.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public static int DeleteAll(IDbTransaction transaction, string tableName = null)
        {
            IDbCommand command = transaction.Connection.CreateCommand();
            command.Transaction = transaction;
            command.CommandText = "DELETE FROM " + (tableName ?? TableName);
            return executeCommand(command, false);
        }

        #endregion Public Methods and Operators
    }
}